using Game.Main.Model;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class NeedleTubingItem : BaseItem
	{
		private float fillAmount;

		private SpriteRenderer waterSprite;

		private Transform pole;

		private ItemComponentTrigger targetItem;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			waterSprite = model.Find("WaterFill").GetComponent<SpriteRenderer>();
			float num = waterSprite.sprite.texture.height;
			Rect textureRect = waterSprite.sprite.textureRect;
			float num2 = textureRect.yMin / num;
			float value = textureRect.yMax / num - num2;
			waterSprite.material.SetFloat("_StartY", num2);
			waterSprite.material.SetFloat("_TotalY", value);
			waterSprite.color = base.itemData.bodyColor;
			pole = model.Find("Pole");
			fillAmount = data.floatParam1 + 1f;
			if (fillAmount != 1f)
			{
				SetFillAmount(fillAmount);
			}
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (targetItem != null)
			{
				SetFillAmount(fillAmount - Time.deltaTime / 4f);
				if (fillAmount <= 0f)
				{
					RecoverNormal();
				}
			}
		}

		protected override void OnItemCompTriggerEnter(ColliderData collData)
		{
			base.OnItemCompTriggerEnter(collData);
			if ((collData.trigger.type == ItemComponentTrigger.Type.LeftHand || collData.trigger.type == ItemComponentTrigger.Type.RightHand) && fillAmount > 0f)
			{
				targetItem = collData.trigger;
				if (targetItem.owner != null && typeof(CharacterItem).IsAssignableFrom(targetItem.owner.GetType()))
				{
					((CharacterItem)targetItem.owner).Cure();
				}
			}
		}

		protected override void OnItemCompTriggerExit(ColliderData collData)
		{
			base.OnItemCompTriggerExit(collData);
			if (collData.trigger == targetItem)
			{
				RecoverNormal();
			}
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			RecoverNormal();
		}

		private void RecoverNormal()
		{
			if (targetItem != null)
			{
				targetItem = null;
			}
		}

		private void SetFillAmount(float val)
		{
			fillAmount = val;
			waterSprite.material.SetFloat("_Fill", fillAmount);
			pole.SetLocalY(0.19f * val + 0.11f);
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["62"] = fillAmount - 1f;
		}
	}
}
